<template lang="pug">
  .login__mod
    h1.login__hd 登陆
    .login__icon
    form.login__form
      .form-group
        input.form-control(type="text", placeholder="请输入账号", name="account", v-model="user")
      .form-group
        input.form-control(type="password", placeholder="请输入密码", name="password", v-model="password")
      .row.form-checked
        .col
          .form-check.form-check-inline
            label.form-check-label
              input.form-check-input(type="checkbox")
              |  记住密码
      button.btn.btn-primary.btn-block.login__submit(type='button', @click="submit") 登陆
      .form__feedback.invalid-feedback.text-center(:class="{'d-block':error}") 请正确输入账号密码
</template>

<script>
export default {
  data () {
    return {
      user: '',
      password: '',
      error: false
    }
  },
  methods: {
    submit () {
      if (!this.user || !this.password) {
        this.error = true
        return
      }
      this.$store.dispatch('login', {
        loginName: this.user,
        password: this.password
      }).then((data) => {
        if (data.roles[0].name === 'teacher') {
          window.location = 'http://120.27.17.206:8070'
        } else {
          this.$router.push('/student')
        }
      }, (e) => {
        alert('登陆失败')
      })
    }
  }
}
</script>
